#! /bin/sh
#chkconfig: 2345 80 90

#------------ Begin init info -------------#
# Provides:          websocket

# Required-Start:    $remote_fs $network

# Required-Stop:     $remote_fs $network

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: starts mail_server

# Description:       starts the PHP FastCGI Process Manager daemon
#------------ End init info -------------#

#php路径，如不知道在哪，可以用whereis php尝试
PHP_BIN=php

#代码根目录
SERVER_PATH=/data/wwwroot/iam

#代码入口
SERVER_MAIN=/bin/hyperf.php

#参数配置 socket_ws 为zphp要加载的配置文件
#PARAMS=socket_ws

#服务名称
PROJCET_NAME=Iam

#获取 masterPid

getMasterPid()

{

    PID=`/bin/ps axu|grep $PROJCET_NAME|grep Master|awk '{print $1}'`
    echo $PID

}

#获取 ManagerPid

getManagerPid()

{

    MID=`/bin/ps axu|grep  $PROJCET_NAME|grep Manager|awk '{print $1}'`
    echo $MID

}

case "$1" in

        start)

                PID=`getMasterPid`

                if [ -n "$PID" ]; then

                    echo -n -e "$PROJCET_NAME server is running. \n"

                    exit 1

                fi

                echo -n -e "Starting $PROJCET_NAME server. \n"

                ####下面一行修改为你的php运行脚本#########

                $PHP_BIN $SERVER_PATH$SERVER_MAIN start

                echo " done."

        ;;



        stop)

                PID=`getMasterPid`

                if [ -z "$PID" ]; then

                    echo -n -e "$PROJCET_NAME server is not running.\n"

                    exit 1

                fi

                echo -n -e "Gracefully shutting down $PROJCET_NAME server. \n"


                kill $PID

                echo "done"

        ;;



        status)

                PID=`getMasterPid`

                if [ -n "$PID" ]; then

                    echo -n -e "$PROJCET_NAME server is running. \n"

                else

                    echo -n -e "$PROJCET_NAME server is not running \n"

                fi

        ;;



        force-quit)

                PID=`getMasterPid`

                if [ -z "$PID" ]; then

                    echo -n -e "$PROJCET_NAME server is not running.\n"

                    exit 1

                fi

                echo -n -e "Gracefully shutting down $PROJCET_NAME server. \n"


                kill $PID

                echo "done"

        ;;



        restart)

                PID=`getMasterPid`

                if [ -z "$PID" ]; then

                    echo -n -e "$PROJCET_NAME server is not running.\n"

                    exit 1

                fi

                echo -n -e "Gracefully shutting down $PROJCET_NAME server. \n"


                kill $PID

                echo "done"

                echo -n -e "Starting $PROJCET_NAME server. \n"

                ####下面一行修改为你的php运行脚本#########

                $PHP_BIN $SERVER_PATH$SERVER_MAIN start

                echo " done."

        ;;



        reload)

                MID=`getManagerPid`

                if [ -z "$MID" ]; then

                    echo -n -e "$PROJCET_NAME server is not running. \n"

                    exit 1

                fi


                echo -n -e "Reload service $PROJCET_NAME \n"



                kill -USR1 $MID



                echo " done"

        ;;



        reloadtask)

                MID=`getManagerPid`

                if [ -z "$MID" ]; then

                    echo -n -e "$PROJCET_NAME server is not running. \n"

                    exit 1

                fi



                echo -n -e "Reload service $PROJCET_NAME. \n"



                kill -USR2 $MID



                echo " done"

        ;;



        *)

                echo "Usage: $0 { start | stop | force-quit | restart | reload | reloadtask |status }"

                exit 1

        ;;



esac


